library(readr)
library(dplyr)
library(stringr)
library(tidyr)
library(ggplot2)
library(chron)

setwd("replication")

extract_sample_indivs <- function(file, n_indiv =100, dev_select = NULL) {
  dt <- str_extract(file, "\\d{8}")
  cat(dt, "\n")
  tune_day <- read_delim(file, 
         delim="|", 
         col_names = c("mso", "device_id", "event_date", "event_time", "event_type", "event_value", "event_name", "event_id"),
         col_types = cols(
            device_id = col_character(),
            event_date = col_date(format="%Y%m%d"),
            event_time = col_time(format="%H%M%S"),
            event_type = col_character())
         )

  devs <- unique(tune_day$device_id)
  if (is.null(dev_select)) dev_select <- sample(devs, n_indiv)
  
  tune_day %>%
    filter(device_id %in% dev_select) %>% 
    arrange(mso, device_id, event_date, event_time) %>%
    group_by(mso, device_id) %>%
    mutate(duration = ifelse(row_number()==n(), 86400 - event_time, lead(event_time) - event_time))
}


### THIS FILE IS PROPRIETARY ###
file <- "data/fwm/FWM_20121214_R.pd.gz"

# sample_tune <- extract_sample_indivs(file)  # sample individuals at random
# filter(sample_tune, event_name == "CNN")    # find someone watching CNN

sample_id <- "0000010fc7c5"
indiv_tune <- extract_sample_indivs(file, sample_id)

format_time <- function(s) {
  H <- s %/% 3600
  M <- (s %% 3600) %/% 60
  S <- s %% 60
  paste(str_pad(H, width=2, pad="0"), str_pad(M, width=2, pad="0"), str_pad(S, width=2, pad="0"), sep=":")
}

indiv_tune <- indiv_tune %>% 
  mutate(
    channel = ifelse(event_value=="65532", "OFF", event_name),
    ch_num = ifelse(event_value=="65532", 0, as.numeric(event_value)),
    channel = factor(channel, levels = unique(channel)[order(unique(ch_num))]),
    start_time = as.numeric(event_time),
    end_time = as.numeric(event_time + duration),
    active = ifelse(channel=="OFF", "Inactive", "Active")
    )


### FIGURE A.2 ###

indiv_timeplot <- ggplot(aes(group=event_id), data=indiv_tune) +
  geom_segment(aes(x = start_time, xend = end_time, y = channel, yend=channel, colour = active), size = 2) + 
  theme_bw() +
  scale_x_continuous(limits = c(-10, 86410), breaks = seq(0,86400,21600), labels=format_time) +
  theme(axis.title.x = element_text(size=20),axis.title.y = element_text(size=20),
        strip.text.x  = element_text(size=20),axis.text.x= element_text(size=14),
        axis.text.y= element_text(size=14),legend.position="bottom",
        legend.text = element_text(size = 20),
        legend.title= element_blank(), legend.background=element_rect(color="black",size=0)) +
  xlab("Time")+ylab("Channel") +ggtitle("12/14/2012 Device ID: 0000010fc7c5")

ggsave(plot = indiv_timeplot, filename="plots/indiv_timeplot.pdf", width=10, height=8)






